package com.hqyj.onlineExamination.modules.exam.dao;

import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import com.hqyj.onlineExamination.modules.exam.entity.Achievement;
import com.hqyj.onlineExamination.modules.exam.entity.Approval;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ApprovalDao {
    @Insert("insert into approval values(null,#{paperId},null,#{status},#{userId})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertApproval(Approval approval);

    @Select("select * from approval where user_id = #{userId} and paper_id = #{paperId}")
    Approval getApproval( Approval approval);

    @Select("select paper_id from approval where user_id = #{userId} and status = '通过' ")
    List<Integer> getApprovalsByUserId(@Param("userId") Integer userId);

    @Select("select * from approval where status = '待审批' ")
    @Results(id = "approvalsResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "paper_id",property = "paper",
                    javaType = com.hqyj.onlineExamination.modules.exam.entity.Paper.class,
                    one = @One(select = "com.hqyj.onlineExamination.modules.exam.dao."+
                            "PaperDao.getPaperById")
            ),
            @Result(column = "user_id",property = "user",
                    javaType = com.hqyj.onlineExamination.modules.account.entity.User.class,
                    one = @One(select = "com.hqyj.onlineExamination.modules.account.dao."+
                            "UserDao.getUserById2")
            )
    })
    List<Approval> getApprovalsBySearchBean(SearchBean searchBean);

    @Select("select * from exam_achievement where user_id = #{userId}")
    List<Achievement> getGradesBySearchBean(SearchBean searchBean, Integer userId);

    @Update("update approval set status = #{status} where id = #{id}")
    int updateApprovalById(Approval approval);
}
